package myky.com.doctorapp.utils.dbhelper;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.NumberFormat;

import myky.com.doctorapp.R;

/**
 * Created by Mr.li on 2018/7/16.
 */

public class UpdateProgressDialog extends Dialog {
    
    private TextView download_Title;
    
    private ProgressBar download_Progress;
    
    private TextView download_Percentage;
    
    private TextView download_Length;

    private Context mContext;

    private String mProgressNumberFormat;

    private Handler mViewUpdateHandler;
    private int          mMax;
    private CharSequence mMessage;
    private boolean      mHasStarted;
    private int          mProgressVal;

    private NumberFormat mProgressPercentFormat;

    public UpdateProgressDialog(Context context) {
        super(context, R.style.MyDialog);
        mContext = context;
        initFormats();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_progress);
        initView();
        mViewUpdateHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int progress = download_Progress.getProgress();
                int max = download_Progress.getMax();
                double dProgress = (double)progress/(double)(1024 * 1024);
                double dMax = (double)max/(double)(1024 * 1024);
                if (mProgressNumberFormat != null) {
                    String format = mProgressNumberFormat;
                    download_Length.setText(String.format(format, dProgress, dMax));
                } else {
                    download_Length.setText("");
                }
                if (mProgressPercentFormat != null) {
                    double percent = (double) progress / (double) max;
                    SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                    tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
                            0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    download_Percentage.setText(tmp);
                } else {
                    download_Percentage.setText("");
                }
            }
        };
        onProgressChanged();
        if (mMessage != null) {
            setMessage(mMessage);
        }
        if (mMax > 0) {
            setMax(mMax);
        }
        if (mProgressVal > 0) {
            setProgress(mProgressVal);
        }
    }

    private void initView() {
        download_Title = (TextView) findViewById(R.id.download_Title);
        download_Progress = (ProgressBar) findViewById(R.id.download_Progress);
        download_Percentage = (TextView) findViewById(R.id.download_Percentage);
        download_Length = (TextView) findViewById(R.id.download_Length);
    }

    private void initFormats() {
        mProgressNumberFormat = "%1.2fM/%2.2fM";
        mProgressPercentFormat = NumberFormat.getPercentInstance();
        mProgressPercentFormat.setMaximumFractionDigits(0);
    }
    private void onProgressChanged() {
        mViewUpdateHandler.sendEmptyMessage(0);


    }
    public void setProgressStyle(int style) {
        //mProgressStyle = style;
    }

    public int getMax() {
        if (download_Progress != null) {
            return download_Progress.getMax();
        }
        return mMax;
    }
    public void setMax(int max) {
        if (download_Progress != null) {
            download_Progress.setMax(max);
            onProgressChanged();
        } else {
            mMax = max;
        }
    }
    public void setIndeterminate(boolean indeterminate) {
        if (download_Progress != null) {
            download_Progress.setIndeterminate(indeterminate);
        }
        //      else {
        //            mIndeterminate = indeterminate;
        //        }
    }
    public void setProgress(int value) {
        if (mHasStarted) {
            download_Progress.setProgress(value);
            onProgressChanged();
        } else {
            mProgressVal = value;
        }
    }

    public void setMessage(CharSequence message) {
        // TODO Auto-generated method stub
        //super.setMessage(message);
        if(download_Title!=null){
            download_Title.setText(message);
        }
        else{
            mMessage = message;
        }
    }


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        mHasStarted = true;
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        mHasStarted = false;
    }
    /**声明成员变量*/
    public OnOkAndCancelListener mOnOkAndCancelListener;
    /**暴露接口取消方法,如果需要确定,另行添加*/
    public interface OnOkAndCancelListener {
        void onCancel(View v);
    }
    /**暴露方法,设置监听*/
    public void setOnOkAndCancelListener(OnOkAndCancelListener mOnOkAndCancelListener) {
        this.mOnOkAndCancelListener = mOnOkAndCancelListener;
    }








}
